/**
 * 
 */
package com.wskj.library.employer.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.wskj.library.pojo.Employer;
import com.wskj.library.service.EmployerService;
import com.wskj.library.service.impl.EmployerServiceImpl;

/**
 * @author 胡洋
 * @describe 
 * @Date 2018年3月26日下午5:58:07
 * @version 1.0
 */
@WebServlet(urlPatterns="/employer/exportExcel.do")
public class EmployerExportExcel extends HttpServlet {
	private EmployerService emService = new EmployerServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//查询所有的员工的信息   导出到Excel 文件中  ....
		List<Employer> employers=emService.queryAllEmployers();
		//通过输出流实现  
		resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
		String filename="员工信息列表.xlsx";
		resp.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(filename,"UTF-8"));  
		ServletOutputStream out = resp.getOutputStream();
		
		Workbook workbook=new XSSFWorkbook();
		Sheet sheet = workbook.createSheet("员工信息列表");
		
		//设置列的宽度 ....
		sheet.setColumnWidth(4, 256*20);
		sheet.setColumnWidth(5, 256*30);
		sheet.setColumnWidth(6, 256*20);
		sheet.setColumnWidth(7, 256*20);
		//设置列时间的样式 ......
		 CellStyle cellStyle = workbook.createCellStyle();
		 cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("yyyy年mm月dd日"));
		    
		
		Row title = sheet.createRow(0);		
		title.createCell(0).setCellValue("序号");
		title.createCell(1).setCellValue("员工工号");
		title.createCell(2).setCellValue("员工名称");
		title.createCell(3).setCellValue("角色");
		title.createCell(4).setCellValue("电话");
		title.createCell(5).setCellValue("邮箱");
		title.createCell(6).setCellValue("生日");
		title.createCell(7).setCellValue("入职时间");
		
	
		for(int i=1;i<=employers.size();i++) {
			Employer em=employers.get(i-1);
			Row row = sheet.createRow(i);
			row.createCell(0).setCellValue(i);
			row.createCell(1).setCellValue(em.getE_number());
			row.createCell(2).setCellValue(em.getE_name());
			row.createCell(3).setCellValue(em.getRname());
			row.createCell(4).setCellValue(em.getE_phone());
			row.createCell(5).setCellValue(em.getE_email());
			Cell cell6 = row.createCell(6);
			Cell cell7 = row.createCell(7);
			
			cell6.setCellStyle(cellStyle);
			cell6.setCellValue(em.getE_birthday());
			cell7.setCellStyle(cellStyle);
			cell7.setCellValue(em.getE_joinday());						
		}		
		//输出流 下载 图书的信息 
		workbook.write(out);
		out.close();
		
	}
	
	
	
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException {
		
		doGet(req, resp);
	};

}
